package com.yougou.ui.address;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yougou.R;
import com.yougou.base.BaseActivity;
import com.yougou.data.model.Address;
import com.yougou.utils.AddressManager;
import com.yougou.utils.ToastUtils;

import java.util.List;

public class AddressListActivity extends BaseActivity {
    
    public static final String EXTRA_SELECT_MODE = "extra_select_mode";
    public static final String EXTRA_SELECTED_ADDRESS = "extra_selected_address";
    
    private RecyclerView mRecyclerView;
    private View mEmptyLayout;
    private Button mBtnAddAddress;
    
    private AddressListAdapter mAdapter;
    private boolean mSelectMode = false; // 是否为选择模式
    
    @Override
    protected void onCreate(android.os.Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_list);
        
        // 获取选择模式
        mSelectMode = getIntent().getBooleanExtra(EXTRA_SELECT_MODE, false);
        
        initView();
        initData();
    }
    
    @Override
    protected void initView() {
        // 设置标题
        if (getSupportActionBar() != null) {
            getSupportActionBar().setTitle(mSelectMode ? "选择收货地址" : "地址管理");
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
        
        mRecyclerView = findViewById(R.id.rv_address);
        mEmptyLayout = findViewById(R.id.layout_empty);
        mBtnAddAddress = findViewById(R.id.btn_add_address);
        
        // 设置列表
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new AddressListAdapter();
        mRecyclerView.setAdapter(mAdapter);
        
        // 设置适配器监听
        mAdapter.setOnAddressActionListener(new AddressListAdapter.OnAddressActionListener() {
            @Override
            public void onAddressClick(Address address) {
                if (mSelectMode) {
                    // 选择模式：返回选中的地址
                    Intent intent = new Intent();
                    intent.putExtra(EXTRA_SELECTED_ADDRESS, address);
                    setResult(RESULT_OK, intent);
                    finish();
                }
            }
            
            @Override
            public void onEditClick(Address address) {
                // 跳转到编辑页面
                Intent intent = new Intent(AddressListActivity.this, AddressEditActivity.class);
                intent.putExtra(AddressEditActivity.EXTRA_ADDRESS, address);
                startActivityForResult(intent, 100);
            }
            
            @Override
            public void onDeleteClick(Address address) {
                AddressManager.deleteAddress(address.getId());
                loadAddresses();
                ToastUtils.show(AddressListActivity.this, "地址已删除");
            }
            
            @Override
            public void onSetDefaultClick(Address address) {
                address.setDefault(true);
                AddressManager.updateAddress(address);
                loadAddresses();
                ToastUtils.show(AddressListActivity.this, "已设为默认地址");
            }
        });
        
        // 添加地址
        mBtnAddAddress.setOnClickListener(v -> {
            Intent intent = new Intent(this, AddressEditActivity.class);
            startActivityForResult(intent, 100);
        });
    }
    
    @Override
    protected void initData() {
        loadAddresses();
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == RESULT_OK) {
            // 刷新地址列表
            loadAddresses();
        }
    }
    
    
    private void loadAddresses() {
        List<Address> addresses = AddressManager.getAddresses();
        if (addresses.isEmpty()) {
            showEmpty();
        } else {
            showList();
            mAdapter.setData(addresses);
        }
    }
    
    
    private void showEmpty() {
        mEmptyLayout.setVisibility(View.VISIBLE);
        mRecyclerView.setVisibility(View.GONE);
    }
    
    
    private void showList() {
        mEmptyLayout.setVisibility(View.GONE);
        mRecyclerView.setVisibility(View.VISIBLE);
    }
    
    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return true;
    }
}

